<template>
  <el-dialog
    :title="!dataForm.jobId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="岗位名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="岗位名称"></el-input>
      </el-form-item>
      <el-form-item label="岗位状态" prop="enabled">
        <el-radio-group v-model="dataForm.enabled">
          <el-radio :label="0">禁用</el-radio>
          <el-radio :label="1">正常</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="排序" prop="jobSort">
        <el-input-number
          v-model.number="dataForm.jobSort"
          :min="0"
          :max="999"
          controls-position="right"
          style="width: 100%;"
        />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        jobId: 0,
        name: "",
        enabled: 1,
        jobSort: 999,
        createBy: "",
        updateBy: "",
        createTime: "",
        updateTime: ""
      },
      dataRule: {
        name: [
          { required: true, message: "岗位名称不能为空", trigger: "blur" },
          {
            min: 1,
            max: 255,
            message: "最多输入255个字符"
          }
        ],
        enabled: [
          { required: true, message: "岗位状态不能为空", trigger: "blur" }
        ],
        jobSort: [{ required: true, message: "排序不能为空", trigger: "blur" }],
        createBy: [
          { required: true, message: "创建者不能为空", trigger: "blur" }
        ],
        updateBy: [
          { required: true, message: "更新者不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建日期不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    init(id) {
      this.dataForm.jobId = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.jobId) {
          this.$http({
            url: this.$http.adornUrl(`/sys/job/info/${this.dataForm.jobId}`),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.name = data.job.name;
              this.dataForm.enabled = data.job.enabled;
              this.dataForm.jobSort = data.job.jobSort;
              this.dataForm.createBy = data.job.createBy;
              this.dataForm.updateBy = data.job.updateBy;
              this.dataForm.createTime = data.job.createTime;
              this.dataForm.updateTime = data.job.updateTime;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/sys/job/${!this.dataForm.jobId ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              jobId: this.dataForm.jobId || undefined,
              name: this.dataForm.name,
              enabled: this.dataForm.enabled,
              jobSort: this.dataForm.jobSort,
              createBy: this.dataForm.createBy,
              updateBy: this.dataForm.updateBy,
              createTime: this.dataForm.createTime,
              updateTime: this.dataForm.updateTime
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
